<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();

$timeTypeList = array(1 => "日", 2 => "週", 3 => "月", 4 => "年");
$resourceTypeList = array(1 => "Shop別", 2 => "Staff別"/*, 3 => "Course別"*/, 4 => "客様別", 5 => "電話番号別", 6 => "指名統計");

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['search_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$day = $getDateArray[2] * 1;

$year = $year > 0 ? $year : $tmp_year;
$month = $month > 0 ? $month : $tmp_mon;
$day = $day > 0 ? $day : $tmp_day;

$timeType = $_GET['time_type'];
$timeType = $timeType < 1 ? 1 : $timeType;
$startTime = mktime(0, 0, 0, $month, $day, $year);

$endTime = null;
if( $timeType == 1 ){
	$endTime = mktime(23, 59, 59, $month, $day, $year);
}else if( $timeType == 2 ){
	$endTime = mktime(23, 59, 59, $month, $day + 6, $year);
}else if( $timeType == 3 ){
	$endTime = mktime(23, 59, 59, $month + 1, $day - 1, $year);
}else{
	$endTime = mktime(23, 59, 59, $month, $day - 1, $year + 1);
}

$resourceId = $_GET['resource_id'];
$resourceType = $_GET['resource_type'];
$resourceType = $resourceType < 1 ? 1 : $resourceType;

$statisticList = array();
if($resourceType == 1){
	$statisticList = Preengage::GetStaticByShopId($startTime, $endTime);
}else if($resourceType == 2){
	$ctitle = "スタッフ名";
	$statisticList = Preengage::GetStaticByStaffId($startTime, $endTime);
}else if($resourceType == 3){
	$statisticList = Preengage::GetStaticByServiceId($startTime, $endTime);
}else if($resourceType == 4){
	$statisticList = Preengage::GetStaticByUserId($startTime, $endTime);
	$ctitle = "お客様";
}else if($resourceType == 5){
	$statisticList = Preengage::GetStaticByTelNo($startTime, $endTime);
	$ctitle = "お客様";
}else if($resourceType == 6){
	$ctitle = "スタッフ名";
	$statisticList = Preengage::GetDesignationedStaticByStaffId($startTime, $endTime);
}

$statisticListView = array();
$totalMoney = array('name' => '合計', 'total_money' => 0);
$serviceList = Service::GetAll();
$serviceIdList = array();
foreach ($serviceList as $k => $v){
	$serviceIdList[$v['id']] = $v;
	$totalMoney[$v['id']] = 0;
}

$shopList = Shop::GetAllShops();
$shopIdList = array();
foreach ($shopList as $k => $v){
	$shopIdList[$v['id']] = $v;
}
if( $statisticList == null ){
	$totalMoney['shop_id'] = 0;
	$totalMoney['count'] = 0;
	$totalMoney['total_money'] = 0;
	$totalMoney['user_id'] = 0;
	
	foreach ($serviceIdList as $k1 => $v1){
		$totalMoney[$k1] = 0;
		$totalMoney[$k1.'_count'] = 0;
	}
}
foreach ($statisticList as $k => $v){
	$v['name'] = $shopIdList[$v['shop_id']]['shop_name'];
	$v['total_money'] = 0;
	
	$totalMoney['count'] = $totalMoney['count'] + $v['count'];
	if($resourceType == 1){
		$tempArray = array();
		foreach ($serviceIdList as $k1 => $v1){
			$temp = Preengage::GetServiceStaticByShopId($v['shop_id'], $k1, $startTime, $endTime);
			$temp['service_name'] = $v1['service_name'];
			$temp['count'] = $temp['count'] * 1;
			$temp['total_money'] = $v1['service_price'] * $temp['count'] + $temp['extra'];
			$v['total_money'] = $v['total_money'] + $temp['total_money'];
			$tempArray[$k1] = $temp;
			$totalMoney[$k1] = $totalMoney[$k1] + $temp['total_money'];
			$totalMoney[$k1.'_count'] = $totalMoney[$k1.'_count'] + $temp['count'];
		}
		$totalMoney['total_money'] = $totalMoney['total_money'] + $v['total_money'];
		$v['services'] = $tempArray;
		array_push($statisticListView, $v);
		
	}else if($resourceType == 2){
		$tempStaff = Staff::getById($v['designation_stuff']);
		$v['title_name'] = $tempStaff['family_name'].' '.$tempStaff['personal_name'];
		$tempArray = array();
		foreach ($serviceIdList as $k1 => $v1){
			$temp = Preengage::GetServiceStaticByStaffId($v['designation_stuff'], $k1, $startTime, $endTime, $v['shop_id']);
			$temp['service_name'] = $v1['service_name'];
			$temp['count'] = $temp['count'] * 1;
			$temp['total_money'] = $v1['service_price'] * $temp['count'] + $temp['extra'];
			$v['total_money'] = $v['total_money'] + $temp['total_money'];
			$tempArray[$k1] = $temp;
			$totalMoney[$k1] = $totalMoney[$k1] + $temp['total_money'];
			$totalMoney[$k1.'_count'] = $totalMoney[$k1.'_count'] + $temp['count'];
		}
		$totalMoney['total_money'] = $totalMoney['total_money'] + $v['total_money'];
		$v['services'] = $tempArray;
		array_push($statisticListView, $v);
	}else if($resourceType == 3){
		$statisticList = Preengage::GetStaticByServiceId($startTime, $endTime);
	}else if($resourceType == 4){
		$tempStaff = Users::GetUser($v['user_id']);
		$v['title_name'] = $tempStaff['family_name'].' '.$tempStaff['personal_name'];
		$tempArray = array();
		foreach ($serviceIdList as $k1 => $v1){
			$temp = Preengage::GetServiceStaticByUserId($v['user_id'], $k1, $startTime, $endTime, $v['shop_id']);
			$temp['service_name'] = $v1['service_name'];
			$temp['count'] = $temp['count'] * 1;
			$temp['total_money'] = $v1['service_price'] * $temp['count'] + $temp['extra'];
			$v['total_money'] = $v['total_money'] + $temp['total_money'];
			$tempArray[$k1] = $temp;
			$totalMoney[$k1] = $totalMoney[$k1] + $temp['total_money'];
			$totalMoney[$k1.'_count'] = $totalMoney[$k1.'_count'] + $temp['count'];
		}
		$totalMoney['total_money'] = $totalMoney['total_money'] + $v['total_money'];
		$v['services'] = $tempArray;
		array_push($statisticListView, $v);
	}else if($resourceType == 5){
		$telNo = $v['back1'];
		$v['title_name'] = $telNo;
		
		$tempArray = array();
		foreach ($serviceIdList as $k1 => $v1){
			$temp = Preengage::GetServiceStaticByTelNo($telNo, $k1, $startTime, $endTime, $v['shop_id']);
			$temp['service_name'] = $v1['service_name'];
			$temp['count'] = $temp['count'] * 1;
			$temp['total_money'] = $v1['service_price'] * $temp['count'] + $temp['extra'];
			$v['total_money'] = $v['total_money'] + $temp['total_money'];
			$tempArray[$k1] = $temp;
			$totalMoney[$k1] = $totalMoney[$k1] + $temp['total_money'];
			$totalMoney[$k1.'_count'] = $totalMoney[$k1.'_count'] + $temp['count'];
		}
		$totalMoney['total_money'] = $totalMoney['total_money'] + $v['total_money'];
		$v['services'] = $tempArray;
		array_push($statisticListView, $v);
	}else if($resourceType == 6){
		$tempStaff = Staff::getById($v['designation_stuff']);
		$v['title_name'] = $tempStaff['family_name'].' '.$tempStaff['personal_name'];
		$tempArray = array();
		foreach ($serviceIdList as $k1 => $v1){
			$temp = Preengage::GetDesigationedStaticByStaffId($v['designation_stuff'], $k1, $startTime, $endTime, $v['shop_id']);
			$temp['service_name'] = $v1['service_name'];
			$temp['count'] = $temp['count'] * 1;
			$temp['total_money'] = $v1['service_price'] * $temp['count'] + $temp['extra'];
			$v['total_money'] = $v['total_money'] + $temp['total_money'];
			$tempArray[$k1] = $temp;
			$totalMoney[$k1] = $totalMoney[$k1] + $temp['total_money'];
			$totalMoney[$k1.'_count'] = $totalMoney[$k1.'_count'] + $temp['count'];
		}
		$totalMoney['total_money'] = $totalMoney['total_money'] + $v['total_money'];
		$v['services'] = $tempArray;
		array_push($statisticListView, $v);
	}
}
$searchUrl = "http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER[ 'QUERY_STRING'];
if( $_GET['response_type'] == "excel" ){
	//Logs::setLog("excel-======startt11111");
	$xls = new PhpExcel('UTF-8', false);
	$tempTitle = array("$ctitle", "店舗名", "合計", "数");
	foreach ($serviceIdList as $k => $v) {
		$tempTitle[$k.'_servicename'] = $v['service_name'];
		$tempTitle[$k.'_total'] = $v['service_name']."(合計)";
	}
	
	$xls->addRow($tempTitle);
	//Logs::setLog("excel-======startt111112");
	
	foreach ($statisticListView as $k => $v){
		$tempContent = array(
			$v['title_name'].$v['designation_stuff'], 
			$v['name'], 
			$v['total_money'], 
			$v['count']);
		foreach ($v['services'] as $k1 => $v1) {
			$tempContent[$k1.'_servicename'] = $v1['count'];
			$tempContent[$k1.'_money'] = $v1['total_money'];
		}
		$xls->addRow($tempContent);
	}
	//Logs::setLog("excel-======startt11113");
	$tempContent = array('',
			$totalMoney['name'], 
			$totalMoney['total_money'], 
			$totalMoney['count']);
	foreach ($serviceIdList as $k1 => $v1) {
		$tempContent[$k1.'_servicename'] = $totalMoney[$k1.'_count'];
		$tempContent[$k1.'_money'] = $totalMoney[$k1];
	}
	//Logs::setLog("excel-======startt11114");
	$xls->addRow($tempContent);
	$fileName = date("y-m-d", $startTime)."～".date("m-d", $endTime)."(".$resourceTypeList[$resourceType].")";
	//Logs::setLog($fileName);
	$xls->generateXML($fileName);
	//Logs::setLog("excel-======over");
	
}else{
	include template('service/data_manage_statistic');
}


//include template('service_staff_index');
//include template('subscribe');
